import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, Index } from "typeorm";
import { ManualTimestamp } from "../decorators/manual-timestamp.decorator";
import { CharacterEntity } from "./character.entity";

@Entity("chat_messages")
@ManualTimestamp()
@Index(["isActive", "created_at"]) // 优化按isActive和created_at查询的性能
@Index(["messageType", "isActive", "created_at"]) // 优化按消息类型查询的性能
@Index(["areaId", "isActive", "created_at"]) // 优化按区域查询的性能
@Index(["factionId", "isActive", "created_at"]) // 优化按帮派查询的性能
export class ChatMessageEntity {
  @PrimaryGeneratedColumn({
    comment: "消息ID" })
  id!: number;

  @Column("text", { comment: "消息内容" })
  content!: string;

  @Column("varchar", { length: 10, comment: "时间显示格式 (如: 10:49)" })
  timeDisplay!: string;

  @Column("varchar", { length: 64, comment: "发送者名称" })
  senderName!: string;

  @Column("int", { comment: "发送者角色ID" })
  senderCharacterId!: number;

  @Column("int", { comment: "消息类型 (1:大区, 4:帮派, 5:商业, 6:系统)" })
  messageType!: number;

  @Column("int", { nullable: true, comment: "区域ID (区域消息专用)" })
  areaId?: number;

  @Column("int", { nullable: true, comment: "帮派ID (帮派消息专用)" })
  factionId?: number;

  @Column("tinyint", { default: 1, comment: "是否有效 (1:有效, 0:已删除)" })
  isActive!: number;

  @Column("int", { default: 0, comment: "消息优先级 (0:普通, 1:重要, 2:紧急)" })
  priority!: number;

  @Column("datetime", { name: "created_at", comment: "创建时间" })
  created_at!: Date;
@Column("datetime", { name: "updated_at", nullable: true, comment: "更新时间" })
  updated_at!: Date | null;
// 关联关系
  @ManyToOne(() => CharacterEntity, { nullable: true })
  @JoinColumn({ name: "senderCharacterId" })
  senderCharacter!: CharacterEntity;
}
